//
//  CZEmoticonKeyBoard.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//重用标记
private let CZEmoticonCollectionViewCellIdentifier = "CZEmoticonCollectionViewCellIdentifier"

class CZEmoticonKeyBoard: UIView {

	private lazy var tooBar: CZEmticonTool = {
		let view = CZEmticonTool(frame: CGRectZero)
		return view
	}()
	private lazy var emoticonCollectionView: UICollectionView = {
	let flowLayout = UICollectionViewFlowLayout()
		flowLayout.scrollDirection = .Horizontal
		
	let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
		//开启分页
		view.pagingEnabled = true
		//去掉滚动条
		view.showsHorizontalScrollIndicator = false
		view.showsVerticalScrollIndicator = false
		view.registerClass(CZEmoctionCollectionViewCell.self, forCellWithReuseIdentifier: CZEmoticonCollectionViewCellIdentifier)
		view.dataSource = self
		view.delegate = self
		view.bounces = false
		view.backgroundColor = self.backgroundColor
		return view
	}()
	
	override init(frame: CGRect) {
		super.init(frame: frame)
		setUpUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	

	
	private func setUpUI() {
		backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
		
	addSubview(tooBar)
		addSubview(emoticonCollectionView)
		
		emoticonCollectionView.snp_makeConstraints { (make) in
			make.top.equalTo(self)
			make.leading.equalTo(self)
			make.trailing.equalTo(self)
			make.bottom.equalTo(tooBar.snp_top)
		}
		
		tooBar.snp_makeConstraints { (make) in
			make.bottom.equalTo(self)
			make.leading.equalTo(self)
			make.trailing.equalTo(self)
			make.height.equalTo(40)
		}
	 tooBar.didSelectedEmoticonButtonClosur = { [weak self] (type: CZEmticonToolBarButtonType) in
		let indexPath: NSIndexPath
		
		switch type {
		case .Normal:
	indexPath = NSIndexPath(forItem: 0, inSection: 0)
			print("Normal")
		case .Emoji:
			print("Emoji")
			indexPath = NSIndexPath(forItem: 0, inSection: 1)
		case .Lxh:
			print("Lxh")
			indexPath = NSIndexPath(forItem: 0, inSection: 2)
		}
		
		//滚动到指定位置
self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
		
		}
	}
	override func layoutSubviews() {
		super.layoutSubviews()
		//获取布局方式
		let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
		flowLayout.itemSize = emoticonCollectionView.size
		//间距水平和垂直
		flowLayout.minimumInteritemSpacing = 0
		flowLayout.minimumLineSpacing = 0
		
	}
}

extension CZEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {

	func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
		return CZEmotionTools.sharedTools.allEmoticons.count
	}
	func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		return CZEmotionTools.sharedTools.allEmoticons[section].count
	}
	
	func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
		let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZEmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! CZEmoctionCollectionViewCell
		cell.indexPath = indexPath
		cell.emicons = CZEmotionTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
		
		return cell
	}
//监听滚动获取 cell
	func scrollViewDidScroll(scrollView: UIScrollView) {
//		print(scrollView)
		
		let contentOffSetX = scrollView.contentOffset.x
		
		//获取当前显示的 cell ,是无序的,需要进行排序,然后判断 cell 哪个显示的多
		let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
			//根据 frame 的 x 进行排序
			return firstCell.x < secondCell.x
		}
		//判断哪个 cell 多
		if cells.count == 2 {
			//获取第一个 cell
			let firstCell = cells.first!
			//获取第二个 cell
			let secondCell = cells.last!
			
			let firstCellContentOffsetX =  abs(firstCell.x - contentOffSetX)
			let secondCellContentOffsetX = abs(secondCell.x - contentOffSetX)
			
			//如果第一个差值小于我们第二个差值,则第一个 cell 显示的多
			let indexPath: NSIndexPath
			
			if firstCellContentOffsetX < secondCellContentOffsetX{
    indexPath = emoticonCollectionView.indexPathForCell(firstCell)!
//			print("第一个显示多")
			} else {
//			print("第二个显示多")
				 indexPath = emoticonCollectionView.indexPathForCell(secondCell)!
			}
			//获取 indexpath 对应的 section
			let section = indexPath.section
			
			//设置按钮选中
			tooBar.selectEmoticonButtonWithSection(section)
		}
	}
	
	
}












